package com.ruoyi.jiajia.controller;

import java.util.List;

import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.IJiajiaMessageService;
import com.ruoyi.system.service.IJiajiaOrderService;
import com.ruoyi.system.service.IJiajiaUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IJiajiaShouHouService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 申请售后Controller
 * 
 * @author knd
 * @date 2021-11-04
 */
@Controller
@RequestMapping("/jiajia/shouHou")
public class JiajiaShouHouController extends BaseController
{
    private String prefix = "jiajia/shouHou";

    @Autowired
    private IJiajiaShouHouService jiajiaShouHouService;

    @Autowired
    private IJiajiaUserService userService;

    @Autowired
    private IJiajiaMessageService messageService;

    @Autowired
    private IJiajiaOrderService orderService;

    @RequiresPermissions("jiajia:shouHou:view")
    @GetMapping()
    public String shouHou()
    {
        return prefix + "/shouHou";
    }

    /**
     * 查询申请售后列表
     */
    @RequiresPermissions("jiajia:shouHou:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JiajiaShouHou jiajiaShouHou)
    {
        startPage();
        List<JiajiaShouHou> list = jiajiaShouHouService.selectJiajiaShouHouList(jiajiaShouHou);
        return getDataTable(list);
    }

    /**
     * 导出申请售后列表
     */
    @RequiresPermissions("jiajia:shouHou:export")
    @Log(title = "申请售后", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JiajiaShouHou jiajiaShouHou)
    {
        List<JiajiaShouHou> list = jiajiaShouHouService.selectJiajiaShouHouList(jiajiaShouHou);
        ExcelUtil<JiajiaShouHou> util = new ExcelUtil<JiajiaShouHou>(JiajiaShouHou.class);
        return util.exportExcel(list, "申请售后数据");
    }

    /**
     * 新增申请售后
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存申请售后
     */
    @RequiresPermissions("jiajia:shouHou:add")
    @Log(title = "申请售后", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JiajiaShouHou jiajiaShouHou)
    {
        return toAjax(jiajiaShouHouService.insertJiajiaShouHou(jiajiaShouHou));
    }

    /**
     * 修改申请售后
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        JiajiaShouHou jiajiaShouHou = jiajiaShouHouService.selectJiajiaShouHouById(id);
        mmap.put("jiajiaShouHou", jiajiaShouHou);
        return prefix + "/edit";
    }

    /**
     * 修改保存申请售后
     */
    @RequiresPermissions("jiajia:shouHou:edit")
    @Log(title = "申请售后", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JiajiaShouHou jiajiaShouHou)
    {
        return toAjax(jiajiaShouHouService.updateJiajiaShouHou(jiajiaShouHou));
    }

    /**
     * 删除申请售后
     */
    @RequiresPermissions("jiajia:shouHou:remove")
    @Log(title = "申请售后", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jiajiaShouHouService.deleteJiajiaShouHouByIds(ids));
    }

    /**
     * 审核
     */
    @RequiresPermissions("jiajia:renZheng:review")
    @PostMapping("/shenHe")
    @ResponseBody
    public AjaxResult shenHe(JiajiaShouHou model)
    {
        JiajiaShouHou shouHou = jiajiaShouHouService.selectJiajiaShouHouById(model.getId());
        if (shouHou == null)return AjaxResult.refreshAgain();
        shouHou.setStatus(model.getStatus());
        shouHou.setBoHuiCause(model.getBoHuiCause());

        JiajiaUser user = userService.selectJiajiaUserById(shouHou.getUid());

        JiajiaMessage message = new JiajiaMessage();
        message.setType(2);
        message.setTitle("售后反馈");
        message.setUid(user.getId());

        String content = shouHou.getStatus().intValue() == 1 ? "您申请的售后已处理" : "您申请的售后被驳回，驳回原因:"+shouHou.getBoHuiCause();

        message.setContent(content);
        messageService.insertJiajiaMessage(message);
        jiajiaShouHouService.updateJiajiaShouHou(shouHou);

        if (shouHou.getStatus().intValue() == 2){
            //可重新售后
            JiajiaOrder order = orderService.selectJiajiaOrderById(shouHou.getOid());
            order.setIsShouHou(0);
            orderService.updateJiajiaOrder(order);
        }

        return toAjax(1);
    }



}
